#include<bits/stdc++.h>
using namespace std;
const int N=8099,M=6e4+99;
int n,m,x[N],y[N],o[N];
int vis[M],p[M],cntp,val[M];
namespace sub1{
    int a[4];
    void solve(){
        long long ans=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                for(int k=j+1;k<=n;k++){
                    a[1]=abs(x[i]-x[j])+abs(y[i]-y[j]);
                    a[2]=abs(x[j]-x[k])+abs(y[j]-y[k]);
                    a[3]=abs(x[k]-x[i])+abs(y[k]-y[i]);
                    sort(a+1,a+3+1);
                    if(val[a[2]])
                        ans+=o[i]+o[j]+o[k];
                }
        cout<<ans<<'\n';
    }
}
namespace sub2{
    bitset<3009> sm[3009],bg[3009],now;
    vector<pair<int,int> > node[M];
    void solve(){
        long long ans=0;
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                node[abs(x[i]-x[j])+abs(y[i]-y[j])].push_back(make_pair(i,j));
        for(int i=1;i<=n;i++){
            sm[i].reset(); bg[i].reset();
            for(int j=1;j<=n;j++){
                if(i!=j)
                    bg[i][j]=1;
            }
        }
        pair<int,int> nd;
        int x,y;
        for(int i=1;i<=m*2;i++){
            while(!node[i].empty()){
                nd=node[i].back(); node[i].pop_back();
                x=nd.first; y=nd.second;
                bg[x][y]=bg[y][x]=0;
                if(val[i]){
                    now=(sm[x]&bg[y]);
                    ans+=now.count()*3;
                    now=(bg[x]&sm[y]);
                    ans+=now.count()*3;
                }
                sm[x][y]=sm[y][x]=1;
            }
        }
        cout<<ans<<'\n';
        return;
    }
}
void solve(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>x[i]>>y[i];
    for(int i=1;i<=n;i++)
        cin>>o[i];
    if(n<=100&&0) sub1::solve();
    else sub2::solve();
    return;
}
void init(){
    int maxn=6e4;
    for(int i=2;i<=maxn;i++){
        if(!vis[i]){
            vis[i]=p[++cntp]=i;
            if(i<=10||i%20==3) val[i]=1;
        }
        for(int j=1;j<=cntp;j++){
            if(i*p[j]>maxn||vis[i]<p[j]) break;
            vis[i*p[j]]=p[j];
        }
    }
    return;
}
bitset<100> xx;
int main(){
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    init();
    int T; cin>>T;
    while(T--) solve();
    return 0;
}